package com.hngy.xpq.android_practice.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.hngy.xpq.android_practice.bean.Wallet;
import com.hngy.xpq.android_practice.utils.CommOrder;
import com.hngy.xpq.android_practice.utils.WalletOrder;

import java.util.ArrayList;
import java.util.List;

public class SQLiteWallet extends SQLiteOpenHelper {           //<钱包>
    private SQLiteDatabase sqLiteDatabase;

    public SQLiteWallet( Context context) {
        super(context, WalletOrder.DATABASE_DATA , null , WalletOrder.DATABASE_INS);
        sqLiteDatabase = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + WalletOrder.DATABASE_TABLE + "("
                + WalletOrder.NOTEPAD_ID + " integer primary key autoincrement,"
                + WalletOrder.NOTEPAD_USER_NAME + " VARCHAR(50),"
                + WalletOrder.NOTEPAD_SINGLE_MONEY + " VARCHAR(20),"
                + WalletOrder.NOTEPAD_WITH_MONEY + " VARCHAR(20),"
                + WalletOrder.NOTEPAD_DES + " VARCHAR(50),"
                + WalletOrder.NOTEPAD_TIME + " VARCHAR(20))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public boolean recharge(Wallet w){      //钱包流水账
        ContentValues values = new ContentValues();
        values.put(WalletOrder.NOTEPAD_USER_NAME,w.getName());
        values.put(WalletOrder.NOTEPAD_SINGLE_MONEY,w.getSingle_money());
        values.put(WalletOrder.NOTEPAD_WITH_MONEY,w.getWith_money());
        values.put(WalletOrder.NOTEPAD_DES,w.getDes());
        values.put(WalletOrder.NOTEPAD_TIME,WalletOrder.getTimeDate());
        return
                sqLiteDatabase.insert(WalletOrder.DATABASE_TABLE,null,values)>0;
    }

    public float QueryWithMoney(String name){       //钱包总余额的查询
        float a = 0;
        String money;
        try{
            Cursor cursor = sqLiteDatabase.query(WalletOrder.DATABASE_TABLE,null,null,null,
                    null,null, WalletOrder.NOTEPAD_USER_NAME + "=" + name);
            if (cursor != null){
                while (cursor.moveToNext()){
                    if (cursor.getString(cursor.getColumnIndex(WalletOrder.NOTEPAD_USER_NAME)).equals(name)){
                        money = cursor.getString(cursor.getColumnIndex(WalletOrder.NOTEPAD_SINGLE_MONEY));
                        int size = money.indexOf("-");
                        money = money.replace("-","");
                        if (size == -1){
                            a = a + Float.valueOf(money);
                        } else {
                            a = a - Float.valueOf(money);
                        }
                    }
                }
                cursor.close();
            } else {
                System.out.println("钱包出现问题");
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return a;
    }

    public List<Wallet> QueryWallet(String name){
        List<Wallet> list = new ArrayList<Wallet>();
        try{
            Cursor cursor = sqLiteDatabase.query(WalletOrder.DATABASE_TABLE,null,null,null,
                    null,null, WalletOrder.NOTEPAD_USER_NAME + "=13838425606" );
            if (cursor != null){
                while (cursor.moveToNext()){
                    Wallet w = new Wallet();
                    if (cursor.getString(cursor.getColumnIndex(WalletOrder.NOTEPAD_USER_NAME)).equals(name)){
                        w.setSingle_money(cursor.getString(cursor.getColumnIndex(WalletOrder.NOTEPAD_SINGLE_MONEY)));
                        w.setTime(cursor.getString(cursor.getColumnIndex(WalletOrder.NOTEPAD_TIME)));
                        w.setDes(cursor.getString(cursor.getColumnIndex(WalletOrder.NOTEPAD_DES)));
                        list.add(w);
                    }
                }
                cursor.close();
            } else {
                System.out.println("钱包查询为空");
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
}
